<template>
  <div id="contianer">
    <iframe
        v-loading="previewLoading"
        id="iframe"
        ref="iframe"
        :src="iframeUrl"
        frameborder="0"
        width="100%"
        height="99.5%"
      ></iframe>
  </div>
</template>
<script>
export default {
  data() {
    return {
        iframeUrl:'',
        previewLoading:false
    };
  },
  created() {
    this.init();
  },
  mounted() {
  
  },
  methods: {
    init() {
        this.previewLoading = true
      this.$http({
        url: "/os/algorithm",
        methods: "GET",
      }).then(({ data }) => {
        console.log(data, "看看回显数据");
        this.iframeUrl = data.data +`&toekn=${localStorage.getItem('token')}`
        // this.iframeUrl = 'https://www.baidu.com/'
        this.previewLoading = false
        
        setTimeout(() => {
          this.setIframeCookie();
        }, 5000);
      });
      
    },
    setIframeCookie() {
      const cookieName = 'os.sid';
      const cookieValue = 's%3AmGJC64wls0qcLNe2chacetfo7G7qPic_.9Myw9EDgRUgMpNRB%2BtWw9BJYsoGImq%2BKSw3Gn%2FsnM9w';
      const iframeWindow = this.$refs.iframe.contentWindow;
      // const cookieValue = document.cookie; // return a array    
      //      console.log(cookieValue, "获取的cookie")
      // if (cookieValue) {
      //   const iframeWindow = this.$refs.iframe.contentWindow;
      //   iframeWindow.postMessage({ cookieName: 'yourCookieName', cookieValue }, '*'); // 发送 Cookie 到 iframe
      // }
      this.injectCookie(iframeWindow, cookieName, cookieValue);
    },
    injectCookie(iframeWindow, cookieName, cookieValue) {
      // 在 iframe 中设置 Cookie
      iframeWindow.document.cookie = `${cookieName}=${cookieValue}; path=/; SameSite=None; Secure`;
      console.log(`Cookie 注入: ${cookieName}=${cookieValue}`);
    },
 
  },
};
</script>
<style lang="scss" scoped>
#contianer {
  width: 100%;
  height: 100%;
  position: absolute;
}
</style>
